<?php
/**
 * ClassName:运费计算处理器
 * @auditor:leijunxiang
 * @time 2025/4/15 16:06
 */

namespace app\tool\freight;

use app\tool\ToolBase;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F;

class FreightCalculateHandle extends ToolBase
{
    //渠道详情对象数组
    protected $channelDetailObjAttr;
    //运费价格对象数组
    protected $freightPriceObjAttr;
    //货代数组
    protected $forwardAttr;
    //仓库名称
    protected $areaName;
    //当前重量
    protected $currentWeight;
    //数量
    protected $num;


    /**
     * (数组)开始计算(货代id数组,仓库名称,当前重量,数量)
     */
    public function startCalculate($forwardArr, $warehouseName, $currentWeight, $num)
    {
        if (empty($forwardArr) || empty($warehouseName) || empty($currentWeight) || empty($num)) {
            return [];
        }
        $arr = [];
        //初始化数据
        $this->initData($forwardArr, $warehouseName, $currentWeight, $num);
        //获取渠道详情对象数组
        $this->getChannelDetailObjAttr();
        //获取运费价格对象数组
        $this->getFreightPriceObjAttr();
        //重新排列
        $this->resortAttr();
        $arr = $this->freightPriceObjAttr;
        //清楚数据
        $this->cleanData();
        return $arr;
    }

    /**
     * 初始化数据
     */
    protected function initData($forwardArr, $warehouseName, $currentWeight, $num)
    {
        //渠道详情对象数组
        $this->channelDetailObjAttr = [];
        //运费价格对象数组
        $this->freightPriceObjAttr = [];
        //货代数组
        $this->forwardAttr = $forwardArr;
        //仓库名称
        $this->areaName = strtoupper($warehouseName);
        //当前重量
        $this->currentWeight = $currentWeight;
        //数量
        $this->num = $num;


    }

    /**
     * 获取渠道详情对象数组
     */
    protected function getChannelDetailObjAttr()
    {
        if (!$this->isGoOn()) {
            return;
        }
        $handle = app(ChannelDetailAttrHandle::class);
        //(渠道详情数组)开始处理(货代id数组,仓库名称,当前重量)
        $this->channelDetailObjAttr = $handle->starthandle($this->forwardAttr, $this->areaName, $this->currentWeight);
        if (empty($this->channelDetailObjAttr)) {
            $this->errCode = $handle->getErrCode();
            $this->errMsg = $handle->getErrMsg();
        }
    }

    /**
     * 获取运费价格对象数组
     */
    protected function getFreightPriceObjAttr()
    {
        if (!$this->isGoOn()) {
            return;
        }
        $handle = app(FreightPriceHandle::class);
        //(运费价格对象数组)开始处理(渠道详情对象数组,数量,当前重量)
        $this->freightPriceObjAttr = $handle->startHandle($this->channelDetailObjAttr, $this->num, $this->currentWeight);
        if (empty($this->freightPriceObjAttr)) {
            $this->errCode = $handle->getErrCode();
            $this->errMsg = $handle->getErrMsg();
        }
    }

    /**
     * 重新排列
     */
    protected function resortAttr()
    {
        if (!$this->isGoOn()) {
            return;
        }
        // 按价格从小到大排序
        usort($this->freightPriceObjAttr, function ($a, $b) {
            $ap = $a['price'];
            $bp = $b['price'];
            // 按价格从小到大排序
            return ($ap > $bp) ? 1 : -1;
        });
    }

    /**
     * 清楚数据
     */
    protected function cleanData()
    {
        //渠道详情对象数组
        $this->channelDetailObjAttr = [];
        //运费价格对象数组
        $this->freightPriceObjAttr = [];
        //货代数组
        $this->forwardAttr = [];
        //仓库名称
        $this->areaName = "";
        //当前重量
        $this->currentWeight = 0;
        //数量
        $this->num = 0;
    }

}